// pages/sort/index.js
var utils = require('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tabindex: 0,
        height: "",
        heightArr: [],
        toview: "",
		imgurl:"https://www.xiangmaihua.com/",
       sort:[]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
		var query = wx.createSelectorQuery();
		//选择id
		var that = this;
		query.selectAll('.proimg').boundingClientRect(function (rect) {
			// console.log(rect.width)
			that.setData({
				height: rect.width + 'px'
			})
		}).exec();
        that.getheight()
		that.getdata()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },
    lefttap: function(e) {
        var that = this
        var index = e.currentTarget.dataset.index
        that.setData({
            toview: 'intoview' + index,
            tabindex: index
        })
    },
    proscroll: function(e) {
        var that = this
        let top = e.detail.scrollTop
        let heightArr = that.data.heightArr
        for (let i = 0; i < heightArr.length; i++) {
            if (top > 0 && top < heightArr[0]) {
                this.setData({
                    tabindex: 0,
                })
			} else if (top >= heightArr[i - 1] && top < heightArr[i]) {
                this.setData({
                    tabindex: i
                })
            }
        }
    },
    getheight: function() {
        var that = this
        var heightArr = that.data.heightArr
		var s=0
        let query = wx.createSelectorQuery();
        query.selectAll(".itemheight").boundingClientRect(function(rect) {
            for (let i = 0; i < rect.length; i++) {
				s += rect[i].height
                heightArr.push(s)
            }
        }).exec();
    },
	gopro: function () {
		wx.navigateTo({
			url: '/pages/product/index',
		})
	},
	getdata:function(){
		var that = this
		utils.GET({
			url: 'Category/category',
			params: {},
			success: function (res) {
				that.setData({
					sort: res.data.d
				})
			},
		})
	}
})